package com.wx.dao.impl;

import com.wx.dao.DrugDao;
import com.wx.domain.Drug;
import com.wx.utils.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/**
 * @author 27250
 */
public class DrugDaoImpl implements DrugDao {
    JdbcTemplate jdbcTemplate=new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public List<Drug> getAllDrug(String pageIndex, String pageSize) {
        return jdbcTemplate.query("select * from drug order by drugId limit ?,?",
                new BeanPropertyRowMapper<>(Drug.class),Integer.parseInt(pageIndex)-1,Integer.parseInt(pageSize));
    }

    @Override
    public List<Drug> getDrugByName(String drugName) {
        System.out.println(drugName);
        return jdbcTemplate.query("select * from drug where instr (drugName,?)>0",
                new BeanPropertyRowMapper<>(Drug.class),drugName);
    }

    @Override
    public void addDrugNumber(String drugId, int inNumber) {
        jdbcTemplate.update("update drug set drugNumber = drugNumber + ? where drugId = ?",inNumber,drugId);
    }

    @Override
    public Drug getDrugByDrugName(String drugName) {
        List<Drug> list = jdbcTemplate.query("select * from drug where drugName = ?",
                new BeanPropertyRowMapper<>(Drug.class), drugName);
        if (list==null || list.size()==0){
            return null;
        }else {
            return list.get(0);
        }
    }

    @Override
    public void addDrug(Drug drug) {
        jdbcTemplate.update("insert into drug  values (null,?,?,?,?,0,?);",
                drug.getDrugName(),drug.getDrugExplain(),drug.getDrugNumber(),drug.getDrugPrice(),drug.getDrugType());
    }
}
